import view.*;
import java.io.*;
import java.util.Scanner;

public class Main {

    private static final String USER_FILE = "C:\\Users\\lenovo\\IdeaProjects\\wang-jingbos-father真的最后了\\项目源码\\src\\users.txt"; // File to store user data
    private static final String POINTS_FILE = "C:\\Users\\lenovo\\IdeaProjects\\wang-jingbos-father真的最后了\\项目源码\\src\\points.txt"; // File to store user points

    public static void main(String[] args) {
        ProductView productView = new ProductView();
        OrderView orderView = new OrderView();
        UserView userView = new UserView();
        EmployeeView employeeView = new EmployeeView(); // New view
        Scanner scanner = new Scanner(System.in);

        // Example menu
        while (true) {
            showMainMenu();
            int choice = scanner.nextInt();
            switch (choice) {
                case 1: // User
                    handleUser(scanner);
                    break;
                case 2: // Admin
                    handleAdmin(scanner);
                    break;
                case 3: // Exit
                    System.out.println("退出系统");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重试");
            }
        }
    }

    private static void handleUser(Scanner scanner) {
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.print("请选择操作: ");
        int option = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over
        switch (option) {
            case 1: // Login
                loginUser(scanner);
                break;
            case 2: // Register
                registerUser(scanner);
                break;
            default:
                System.out.println("无效选择，请重试");
        }
    }

    private static void handleAdmin(Scanner scanner) {
        managerPassword();
        int attempts = 3;
        while (true) {
            int password = scanner.nextInt();
            if (password == 14205) {
                System.out.println("密码正确,已进入管理员界面");
                guanliView.guanliMenu();
                break;
            } else {
                attempts--;
                System.out.println("密码错误，还剩" + attempts + "次机会");
                if (attempts == 0) {
                    break;
                }
            }
        }
    }

    private static void loginUser(Scanner scanner) {
        userName();
        int ID = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over
        userPassword();
        int password = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over

        if (verifyUser(ID, password)) {
            System.out.println("登录成功,祝您购物愉快");
            OrderView.setCurrentUser(String.valueOf(ID));
            OrderView.loadPointsFromFile(POINTS_FILE);
            OrderView.displayMenu();
        } else {
            System.out.println("登录失败，用户ID或密码错误");
        }
    }

    private static void registerUser(Scanner scanner) {
        System.out.println("请输入新用户ID：");
        int newID = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over
        System.out.println("请输入新用户密码：");
        int newPassword = scanner.nextInt();
        scanner.nextLine(); // Consume newline left-over

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(USER_FILE, true))) {
            writer.write(newID + " " + newPassword);
            writer.newLine();
            System.out.println("用户注册成功");
        } catch (IOException e) {
            System.out.println("无法保存用户数据: " + e.getMessage());
        }
    }

    private static boolean verifyUser(int ID, int password) {
        try (BufferedReader reader = new BufferedReader(new FileReader(USER_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                int storedID = Integer.parseInt(parts[0]);
                int storedPassword = Integer.parseInt(parts[1]);
                if (storedID == ID && storedPassword == password) {
                    return true;
                }
            }
        } catch (IOException e) {
            System.out.println("无法读取用户数据: " + e.getMessage());
        }
        return false;
    }

    static void showMainMenu() {
        System.out.println("\n======== 收银台系统 ========");
        System.out.println("1. 用户");
        System.out.println("2. 管理员");
        System.out.println("3. 退出");
        System.out.print("请选择操作: ");
    }

    static void userPassword() {
        System.out.println("请输入用户密码:");
    }

    static void userName() {
        System.out.println("请输入ID：");
    }

    static void managerPassword() {
        System.out.println("请输入管理员密码:");
    }
}
